/* HttpRequestDemo.java 
* Created on Dec 20, 2012 
* Author:Mr.Viet
* Phone: 01683430426
* Email: phamvanvietcdhd@gmail.com
*/ 
package com.session8; 

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpRequestDemo {
	public static void main(String[] args) {
		try {
			URL url = new URL("http://localhost/doAdd.php");
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			conn.setRequestProperty("charset", "UTF-8");
			conn.setDoInput(true);
			conn.setDoOutput(true);
			String data = "user=Phamvanviet&password=123";
			conn.setRequestProperty("Content-Length", data.length()+"");
			conn.connect();
			
			//Gui Du Lieu Di
			OutputStream os = conn.getOutputStream();
			os.write(data.getBytes());
			os.flush();
			os.close();
			
			//Nhan Du Lieu Ve
			InputStream in = conn.getInputStream();
			byte[] b = new byte[in.available()];
			in.read(b);
			System.out.println(new String(b));
			
			//Dang Ket Noi
			conn.disconnect();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
